package com.aaa.system.service.impl;

import com.aaa.entity.DictData;
import com.aaa.entity.User;
import com.aaa.system.dao.DictDataDao;
import com.aaa.system.dao.DictTypeDao;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.IDictTypeService;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aaa.entity.DictType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-03-21
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeDao, DictType> implements IDictTypeService {

    @Resource
    private DictTypeDao dictTypeDao;

    @Resource
    private UserDao userDao;

    @Resource
    private DictDataDao dictDataDao;


    @Override
    public Result<IPage<DictType>> selectAll(Integer current, Integer size, DictType dictType) {
        IPage<DictType> page = new Page<>(current,size);
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(dictType.getDictName())){
            queryWrapper.like("dict_name",dictType.getDictName());
        }
        if (StringUtils.hasText(dictType.getDictType())){
            queryWrapper.like("dict_type",dictType.getDictType());
        }
        if (Objects.nonNull(dictType.getStatus())){
            queryWrapper.eq("status",dictType.getStatus());
        }
        if (Objects.nonNull(dictType.getDateRange()) && dictType.getDateRange().length==2){
            queryWrapper.between("create_time",dictType.getDateRange()[0],dictType.getDateRange()[1]);
        }
        IPage<DictType> dictTypeIPage = dictTypeDao.selectPage(page,queryWrapper);
        return new Result<>(200,"查询所有字典类型成功",dictTypeIPage);
    }

    @Transactional
    @Override
    public Result<Boolean> saveOrUpdateDictType(DictType dictType) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenInfo = JWTUtils.getInfo(token);
        String phone = tokenInfo.get("username").toString();
        //通过token获取登录用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User userByToken = userDao.selectOne(queryWrapper);

        if (Objects.nonNull(dictType.getDictId())){
            //如果字典类型的数据不为空，那么就是修改字典类型
            //1、修改dictData表中的值
            DictType dictType1 = dictTypeDao.selectById(dictType.getDictId());
            //如果修改前的字典类型和修改后的字典类型不相同，就修改DictData表中的数据
            if (!dictType1.getDictType().equals(dictType.getDictType())){
                String beforeDict = dictType1.getDictType();
                String afterDict = dictType.getDictType();
                dictDataDao.updDictType(beforeDict,afterDict);
            }
            //2、修改dictType表中的值
            dictType.setUpdateTime(LocalDateTime.now());
            dictType.setUpdateBy(userByToken.getUserName());
            int i = dictTypeDao.updateById(dictType);
            return new Result<>(200,"修改成功",i>0?true:false);
        }else {
            QueryWrapper<DictType> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dict_type",dictType.getDictType());
            queryWrapper1.eq("dict_name",dictType.getDictName());
            List<DictType> dictTypes = dictTypeDao.selectList(queryWrapper1);
            //如果有同名的字典类型，那么就是添加失败
            if (dictTypes.size()>0){
                return new Result<>(200,"添加失败");
            }else {
                //如果字典类型的数据为空，那么就是添加字典类型
                dictType.setCreateTime(LocalDateTime.now());
                dictType.setCreateBy(userByToken.getUserName());
                int insert = dictTypeDao.insert(dictType);
                return new Result<>(200,"添加成功",insert>0?true:false);
            }
        }
    }

    @Override
    public Result<Boolean> deleteDictTypeByIds(Long[] dictIds) {
        Boolean b = null;
        if (dictIds.length>0){
            b = dictTypeDao.batchByIds(dictIds);
        }
        return new Result<>(200,"删除成功",b);
    }

    @Override
    public Result<List<DictType>> dictTypeList() {
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<DictType> dictTypes = dictTypeDao.selectList(queryWrapper);
        return new Result<>(200,"查询所有字典类型成功",dictTypes);
    }

    @Override
    public Result<String> getTypeById(Long dictId) {
        if (Objects.nonNull(dictId)){
            DictType type = dictTypeDao.selectById(dictId);
            return new Result<String>(200,"查询字典类型",type.getDictType());
        }
        return new Result<>(200,"查询字典类型失败");
    }
}
